package com.mapfinal.server.service.tile;

import java.io.IOException;

import javax.servlet.ServletOutputStream;

import com.jfinal.log.Log;
import com.jfinal.render.Render;
import com.lambkit.common.util.ImageUtils;

public class TileByteRender extends Render {
	private static Log log = Log.getLog(TileByteRender.class);

    private byte[] bytes;
    private String imageType;

    public TileByteRender(byte[] bytes, String imageType) {
        this.bytes = bytes;
        this.imageType = imageType;
    }

    public void render() {
        ServletOutputStream sos = null;
        try {
            response.setHeader("Pragma", "no-cache");
            response.setHeader("Cache-Control", "no-cache");
            response.setDateHeader("Expires", 0);
            response.setContentType(ImageUtils.getMineTypeFormat(imageType));
            sos=response.getOutputStream();
            sos.write(bytes);
        } catch (Exception e) {
            log.error("ByteRender出错:"+e.getLocalizedMessage(),e);
            throw new RuntimeException(e);
        } finally {
            if (sos != null)
                try {
                    sos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
        }
    }
}
